// main layer
var g_mainLayer=null;
var MainLayer = cc.Layer.extend({
    _jsonMap:null,
    _jsonObject : null,
    ctor:function () {
        this._super();
        ClearAllBuffer();
        // add map
        this.addTouchEvent();
        this._jsonObject=CreateJsonWithFile("res/map/map0.json");
        {
            var sprite = new cc.Sprite( "res/background.jpg", cc.rect(0, 0, 960, 640) );
            sprite.setPosition( cc.p( cc.winSize.width*0.5,cc.winSize.height*0.5 ) );
            this.addChild(sprite,0);
        }
        // add map
        if(1){
            this._jsonMap = JsonMap.createWithJsonObject(this._jsonObject);
            this.addChild( this._jsonMap,1 );
        }
        // add role
        if(1){
            var role = Role.create( "res/role.png" );
            role.setPosition( cc.p( cc.winSize.width - 140,120 ) );
            this.addChild(role,2);
        }
        // add eni enemy
        if(1){
            var filename = "res/enemy/ene0.json";
            this.addChild( new emiEnemy(filename),2 );
        }
        // add ui layer
        if(1){
            var money = this._jsonObject.money;
            var uiLayer = new UILayer(money);
            this.addChild(uiLayer);
        }
        // add tower manager
        if(1){
            var filename = "res/configs/towerConfig.json";
            this.addChild( new TowerManager(filename),5 );
        }

        g_mainLayer=this;
        return true;
    },
    onEnter:function () {
        this._super();
        //this.scheduleUpdate();

        //this.starting();
    },
    addTouchEvent : function() {
        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            onTouchBegan: this.onTouchBegan
        }, this);
    },
    onTouchBegan : function (touch, event) {
        var towerManager = getTowerManagerInstance();
        if(towerManager){towerManager.showTowers(touch);}
    },
    starting : function(){
        getEmiEnemyInstance().emission();
    },
    update : function(dt){
        for ( var i=0;i<ENEMY_BUFFER.length;++i) {
            var enemy = ENEMY_BUFFER[i];
            if (enemy._life) {
                var enePos = enemy.getPosition();
                // checking direct col with point
                if(1){
                    var directArr = this._jsonMap.getDirectArray();
                    for ( var d=0;d<directArr.length;d++){
                        var direct = directArr[d];
                        var directType = direct.direct();
                        if( directType!=enemy._directType && cc.rectContainsPoint( direct.collideRect(
                            direct.getPositionX(),direct.getPositionY()),enePos )  ){
                            cc.log("orig direct type: "+enemy._directType);
                            enemy._directType = directType;
                            enemy.direct( directType );
                            cc.log("coll direct type: "+enemy._directType);
                        }
                    }
                }
                // coll with role
                if(1){
                    var role = getRoleInstance(),rolPos = role.getPosition();
                    if ( cc.rectIntersectsRect( enemy.collideRect(enePos.x,enePos.y),role.collideRect(rolPos.x,rolPos.y)) ){
                        cc.log("col role ");
                        enemy.dead(i);
                        role.hurt();
                    }
                }
                // checking weapon is should fire
                for ( var w=0;w<TOWER_BUFFER.length;++w){
                    var weapon = TOWER_BUFFER[w];
                    if (weapon){
                        weapon.fighting( enemy.getPosition() );
                    }
                }
               enemy.moving(dt);
            }
        }
        // update bullet
        for ( var b=0;b<BULLET_BUFFER.length;++b){
            var bullet = BULLET_BUFFER[b];
            if ( bullet&&bullet.isVisible() ){
                bullet.moving( dt );
                for ( var i=0;i<ENEMY_BUFFER.length;++i) {
                    var enemy = ENEMY_BUFFER[i];
                    if (enemy && enemy._life) {
                        var enePos = enemy.getPosition(),bulPos = bullet.getPosition();
                        if ( bullet && cc.rectIntersectsRect( enemy.collideRect(enePos.x,enePos.y),bullet.collideRect(bulPos.x,bulPos.y)) ){
                            cc.log("bullet collision with enemy" );
                            bullet.setVisible(false);
                            var enPos = enemy.getPosition();
                            if ( enemy.hurt(bullet.getPower(),i) ){
                                playEnemyDeadEffect( enPos );
                            }
                        }
                    }
                }
            }
        }
    }
});

